package com.dmai.capacity.platform.user.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;


import com.dmai.capacity.platform.common.constant.CodeEnum;
import com.dmai.capacity.platform.common.constant.CommonConstant;
import com.dmai.capacity.platform.common.dto.PageResultData;
import com.dmai.capacity.platform.common.exception.BusinessException;
import com.dmai.capacity.platform.common.model.SysRole;
import com.dmai.capacity.platform.user.mapper.*;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.dmai.capacity.platform.common.dto.PageResult;
import com.dmai.capacity.platform.user.service.ISysRoleService;

import lombok.extern.slf4j.Slf4j;


@Slf4j
@Service
public class SysRoleServiceImpl implements ISysRoleService {

    @Autowired
    private SysRoleDao sysRoleDao;
    @Autowired
    private SysScopePermissionDao scopePermissionDao;
    @Autowired
    private SysFunctionPermissionDao functionPermissionDao;
    @Autowired
    private SysUserRoleDao userRoleDao;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(SysRole sysRole) {
        SysRole role = sysRoleDao.findByCode(sysRole.getCode());
        if (role != null) {
            throw new IllegalArgumentException("角色code已存在");
        }

        sysRole.setCreateTime(new Date());
        sysRole.setUpdateTime(sysRole.getCreateTime());

        sysRoleDao.save(sysRole);
        log.info("保存角色：{}", sysRole);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(SysRole sysRole) {
        sysRole.setUpdateTime(new Date());

        sysRoleDao.updateByOps(sysRole);
        log.info("修改角色：{}", sysRole);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteRole(Long id) {
        SysRole orig = sysRoleDao.findById(id);

        if (orig == null) {
            throw new IllegalArgumentException("找不到对应的数据");
        }

        sysRoleDao.delete(id);
//        scopePermissionDao.deleteByObjectId(id.toString());
//        functionPermissionDao.deleteByObjectId(id.toString());

        log.info("删除角色：{}", orig);

    }


    @Override
    public SysRole findById(Long id) {
        return sysRoleDao.findById(id);
    }

    @Override
    public SysRole findByCode(String code) {
        return sysRoleDao.findByCode(code);
    }

    @Override
    public List<SysRole> findRoles(Map<String, Object> params) {
        return sysRoleDao.findList(params);
    }


    @Override
    public PageResult<SysRole> pageRoles(Map<String, Object> params) {
        Page<SysRole> startPage = null;
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, CommonConstant.PAGE_CURRENT) != null && MapUtils.getInteger(params, CommonConstant.PAGE_SIZE) != null) {
            startPage = PageHelper.startPage(MapUtils.getInteger(params, CommonConstant.PAGE_CURRENT), MapUtils.getInteger(params, CommonConstant.PAGE_SIZE), true);
            startPage.setOrderBy("code");
        }

        List<SysRole> list = sysRoleDao.findList(params);
        PageInfo<SysRole> pageInfo = new PageInfo(list);

        return PageResult.<SysRole>builder().data(PageResultData.<SysRole>builder().records(pageInfo.getList()).total(pageInfo.getTotal()).pages(pageInfo.getPages()).build()).code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();

    }


    @Override
    public boolean saveOrUpdate(SysRole sysRole, Long keyValue) {
        int i;
        if (keyValue == null || keyValue.equals(0)) {
            SysRole role = sysRoleDao.findByCode(sysRole.getCode());

            if (role != null) {
                throw new BusinessException("角色code已存在");
            }
            //sysRole.setId(UUIDUtils.getOriginalUUIDWithoutBar());

            sysRole.setCreateTime(new Date());
            sysRole.setUpdateTime(sysRole.getCreateTime());
            i = sysRoleDao.save(sysRole);
        } else {
            SysRole role = sysRoleDao.findByCode(sysRole.getCode());
            if (role != null && !role.getId().equals(keyValue)) {
                throw new BusinessException("角色code已存在");
            }
            sysRole.setUpdateTime(new Date());
            i = sysRoleDao.updateByOps(sysRole);
        }
        return i > 0;
    }


}
